package com.jwthhealth_pub.community.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.community.presenter.ICommunityPresenter;
import com.jwthhealth_pub.community.presenter.ICommunityPresenterCompl;
import com.jwthhealth_pub.community.view.adapter.CommunityDocAdaper;
import com.jwthhealth_pub.community.view.adapter.CommunityDocTagAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;

/**
 * 找医生
 * Created by liuzhen on 2017/12/17.
 */

public class CommunityActivity extends BaseActivity implements ICommunityPresenter.view {

    private static final String TAG = makeLogTag(CommunityActivity.class);

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.rv_community_docstags)
    RecyclerView rvCommunityDocsTags;
    @BindView(R.id.rv_community_docs)
    RecyclerView rvCommunityDocs;
    private ICommunityPresenter.presetner mPresenter;
    private CommunityDocAdaper mDocAdapter;
    private CommunityDocTagAdapter communityDocTagAdapter;

    @Override
    protected void title() {
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_community);
        ButterKnife.bind(this);

        // TODO: 2017/12/17 abstact ???
        new ICommunityPresenterCompl(this);
    }

    @Override
    public void showDocTags() {
        rvCommunityDocsTags.setLayoutManager(new GridLayoutManager(this, 2));

        String[] docTags = new String[]{"全部", "主任医师", "健康管理师（初级）", "副主任医师", "健康管理师（高级）",
                "主治医师", "健康管理师（初级）", "助理医师"};

        communityDocTagAdapter = new CommunityDocTagAdapter(this, docTags);
        rvCommunityDocsTags.setAdapter(communityDocTagAdapter);
    }

    @Override
    public void showDocs() {
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Object());
        }
        rvCommunityDocs.setLayoutManager(new LinearLayoutManager(this));
        mDocAdapter = new CommunityDocAdaper(this, list);
        rvCommunityDocs.setAdapter(mDocAdapter);
    }

    @Override
    public void showDocInfo() {
        final BottomSheetDialog shopbottomSheetDialog = new BottomSheetDialog(this);
        View view = View.inflate(this, R.layout.item_community_doc_info, null);
        shopbottomSheetDialog.setContentView(view);
        Button closeBtn = view.findViewById(R.id.btn_community_doc_close);

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shopbottomSheetDialog.dismiss();
            }
        });

        shopbottomSheetDialog.show();
    }


    // TODO: 2017/12/17 多余 ???
    @Override
    public void setmPresenter(ICommunityPresenter.presetner presenter) {
        mPresenter = presenter;
    }

    public void refreshDecTags() {
        if (communityDocTagAdapter == null) return;

//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                communityDocTagAdapter.notifyDataSetChanged();
//                Log.d(TAG, "notify");
//            }
//        });

        rvCommunityDocs.post(new Runnable() {
            @Override
            public void run() {
                communityDocTagAdapter.notifyDataSetChanged();
            }
        });
    }

    public void gotoApplyPage() {
        startActivity(new Intent(this, CommunityApplyActivity.class));
    }
}
